
//
//  toupiaoViewController.m
//  hainingOASystem
//
//  Created by michael on 2017/12/11.
//  Copyright © 2017年 czd. All rights reserved.
//
#import "toupiaoxqViewController.h"
#import "toupiaoViewController.h"
#import "toupiaoresultTableViewCell.h"
#import "toupiaotopCollectionViewCell.h"
#import "toupiaolistCollectionViewCell.h"
@interface toupiaoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UILabel *labelOne;
}
@property(nonatomic,strong)UICollectionView *collection;

@end

@implementation toupiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"投票页面";
    self.view.backgroundColor = [UIColor whiteColor];
       [self makeUI];
       [self requestdata];
    // Do any additional setup after loading the view.
}
 -(void)requestdata{
 _id = @"7";
[MBProgressHUD showHUDAddedTo:self.view animated:YES];

 NSString *urlstring = [NSString stringWithFormat:@"%@%@",ktoupiaolist,_id];
  [httpTool ZBPostNetDataWith:urlstring withDic:nil andSuccess:^(NSDictionary* dictionary){
  [MBProgressHUD hideHUDForView:self.view animated:YES];

  _topdic = dictionary[@"votePeriods"];
  NSArray *array = dictionary[@"OptionInfoList"];
  _dowmarray = [NSMutableArray array];
_dowmarray = [array mutableCopy];

[_collection reloadData];

    }
 andFailure:^{
 [self.view makeToast:@"服务器错误!"];
  [MBProgressHUD hideHUDForView:self.view animated:YES];

 }];

 }
#pragma mark -------- 界面
-(void)makeUI{

    labelOne = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 20)];
    UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.sectionInset =UIEdgeInsetsMake(0, 0, 0, 0);
    layout.headerReferenceSize =CGSizeMake(KScreenWidth, 30);
        _collection =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-kTopHeight-kTabBarHeight) collectionViewLayout:layout];
        _collection.delegate =self;
    _collection.dataSource =self;
    _collection.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_collection];
        [_collection registerClass:[toupiaotopCollectionViewCell
         class] forCellWithReuseIdentifier:@"toupiaotopid"];
    [_collection registerClass:[toupiaolistCollectionViewCell class] forCellWithReuseIdentifier:@"toupiaolistid"];
     // 注册头视图
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];



}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0) {
        toupiaotopCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"toupiaotopid" forIndexPath:indexPath];
        cell.dic = _topdic;
        [cell setNeedsLayout];
        return cell;
        
    }else{
        
        toupiaolistCollectionViewCell*cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"toupiaolistid" forIndexPath:indexPath];
        cell.button.tag = indexPath.row;
        cell.dic = _dowmarray[indexPath.row];
                [cell setNeedsLayout];

        [cell.button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
      NSString *string4 = _topdic[@"title"];
        CGFloat titleSize4 = [OAToolBox sizebyText:string4 font:13 minsWidth:100];
NSString *string6 = _topdic[@"content"];

        CGFloat titleSize6 = [OAToolBox sizebyText:string6 font:13 minsWidth:100];
        return CGSizeMake(KScreenWidth,titleSize6+titleSize4+150+10+30+10);
    }else{
        
        return CGSizeMake((KScreenWidth - 10) / 2, 240);
        
    }
}
//头视图的大小   COLORRGB(0x0F52C1)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  if (section==1) {
      
    return CGSizeMake(KScreenWidth,30);
    }else{
        
        return CGSizeZero;
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        
        return 1 ;
    }else{
        
        return _dowmarray.count;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
#pragma mark ----- 重用的问题
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
   
    header.backgroundColor = RGB(236, 237, 241);
    
    if (indexPath.section == 1) {
        labelOne.text = @"投票名单";
        labelOne.font = [UIFont systemFontOfSize:14.0f];
        labelOne.textColor =COLORRGB(0x0F52C1);
        [header addSubview:labelOne];
            return header;

    }else{
    
                return nil;

    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
 NSDictionary  *dic = _dowmarray [indexPath.row];

      NSLog(@"%zd",indexPath.item);
    toupiaoxqViewController *vc = [[toupiaoxqViewController alloc]init];
    vc.id = [NSString stringWithFormat:@"%@",dic[@"id"]];
    [self.navigationController pushViewController:vc animated:YES];
}
  
}
- (void)action:(UIButton *)button{
 NSDictionary  *dic = _dowmarray [button.tag];

    NSLog(@"-----%ld-----",(long)button.tag);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSString *toupiaoid = [NSString stringWithFormat:@"%@",dic[@"id"]];
 NSString *urlstring = [NSString stringWithFormat:@"%@%@",ktoupiao,toupiaoid];
  [httpTool ZBPostNetDataWith:urlstring withDic:nil andSuccess:^(NSDictionary* dictionary){
  [MBProgressHUD hideHUDForView:self.view animated:YES];

 if ([[NSString stringWithFormat:@"%@",dictionary[@"success"]]isEqualToString:@"true"]) {
           [self .view makeToast:dictionary[@"msg"]];
 
     
}else{
NSString *string = dictionary[@"msg"];
           [self .view makeToast:string ];

}

    }
 andFailure:^{
  [MBProgressHUD hideHUDForView:self.view animated:YES];
 [self.view makeToast:@"服务器错误!"];

 }];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
